/**
 * 
 */

var busyIndicator = null;

projects = {};
var project_list = null;
$(document).on("pagebeforeshow", "#allProjects", function(event) {
	WL.Logger.debug("allProjects::on::pagebeforeshow");
	busyIndicator = new WL.BusyIndicator("content", {
		text : "Loading..."
	});
	
	projects.listAllProjects();
});

projects.listAllProjects = function() {

	 busyIndicator.show();
	WL.Logger.debug("listAllProjects::invokeProcedure");
	var invocationData = {
		adapter : "Redmine",
		procedure : "getProjects",
		parameters : []
	};
	// busyIndicator.show();
	WL.Client
			.invokeProcedure(
					invocationData,
					{
						onSuccess : function(response) {

								if (response.invocationResult.statusCode == 200) {
									WL.Logger
											.debug("listAllProjects::invokeProcedure::Successful");
									project_list = response.invocationResult.projects;
									projects.renderProjectList(project_list);
								} else
									alert("Invoke getProjects Procedure Failure.StatusCode!=200. Response :: "
											+ JSON.stringify(error));
							
						},
						onFailure : function(response) {
							if (response.invocationResult.authStatus == "required") {
								customAuthenticatorRealmChallengeHandler
										.handleChallenge(response);
							}
							else {
							WL.Logger
									.debug("projects.listAllProjects::invokeProcedure::Failure. Error = "
											+ JSON.stringify(error));
							alert("Invoke getProjects Procedure Failure. Response :: "
									+ JSON.stringify(error));
							}
						}
					});
};

projects.renderProjectList = function(project_list) {
	WL.Logger
	.debug("listAllProjects::renderProjectList");
	var $projects_listview = $('#project_list');
	var $template = $('#project_list .template');
	var $project_item_template = $('#project_list .sample-project-item')
			.clone().removeClass('sample-project-item template').addClass('project-item').show();
	$projects_listview.html('');
	
	
	
	if (!project_list || project_list.length == 0) {
		//$("#project_list .no-issues").show();
		$template.appendTo($projects_listview).filter('.no-projects').show();
	} else {
		$.each(project_list, function(i, project) {

			var project_id = project.id;
			var project_name = project.name;
			
			var $project_item = $project_item_template.clone().attr({
				"project-id" : project_id,
				"project-index" : i
			});
			$project_item.find(".project-link").append(project_name);
			$projects_listview.append($project_item);

		});
		$projects_listview.listview('refresh').append(
				$template.hide());
	}
	 busyIndicator.hide();

};
$(document).on("pageshow", "#allProjects", function() {
	$('#project_list').on('tap', '.project-item', function(e) {
		e.stopImmediatePropagation();
		e.preventDefault();
		//e.stopImmediatePropagation();
		//e.preventDefault();
		var idx = $(this).attr("project-index");
		
	//	sessionStorage.selectedProject = {"id" : project_list[idx].id , "name" : project_list[idx].name };
		sessionStorage.selectedProjectId = project_list[idx].id;
		sessionStorage.selectedProjectName = project_list[idx].name;
	//	$.mobile.changePage('ProjectIssues.html', {changeHash : false,transition: "slide"});
		pageHistory.navigateToPage("projectIssues",{transition: "slide"});
	});
	
	$('.refresh-button').bind('click', function() { 
		pageHistory.navigateToPage("allProjects",{transition: "slide", allowSamePageTransition : true });
	});

	
});

